package com.chenxin.wms.controller;

import com.chenxin.common.constant.WmsConstants;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.wms.domain.dto.*;
import com.chenxin.wms.process.inbound.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wms/ibRegister")
public class IbRegisterController extends BaseController {
    @Autowired
    private IProcWipMaterialRegister wipMaterialRegister;
    @Autowired
    private IProcDeliverRegister deliverRegister;

    @Autowired
    private IContNoReleaseProcess wipMaterialRelease;

    @Autowired
    private IProcWorkMaterialReturn workMaterialReturn;

    @Autowired
    private IProcFinishedGoodsRegister finishedGoodsRegister;

    @PostMapping("/deliverRegister")
    public AjaxResult deliverRegister(@RequestBody DeliverRegisterParams params) {
        // 调用送货单收货登记
        ProcResult procResult = deliverRegister.call(params);
        if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
            return AjaxResult.success(procResult.getMessage());
        } else {
            return AjaxResult.error(procResult.getMessage());
        }
    }

    @PostMapping("/wipRegister")
    public AjaxResult goRegister(@RequestBody WipMaterialRegisterParams dto) {
        ProcResult procResult = wipMaterialRegister.call(dto);
        if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
            return AjaxResult.success(procResult.getMessage());
        } else {
            return AjaxResult.error(procResult.getMessage());
        }
    }

    @PostMapping("/returnRegister")
    public AjaxResult workReturnMaterialRegister(@RequestBody WorkMaterialReturnParams dto) {
        try {
            ProcResult procResult = workMaterialReturn.executeProcess(dto);
            if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
                return AjaxResult.success(procResult.getMessage());
            } else {
                return AjaxResult.error(procResult.getMessage());
            }
        } catch (Exception e) {
            return AjaxResult.error("处理车间退料入库登记请求时发生异常: " + e.getMessage());
        }
    }

    /**
     * 成品收货登记
     */
    @PostMapping("/finishedGoodsRegister")
    public AjaxResult finishedGoodsRegister(@RequestBody FinishedGoodsRegisterParams dto) {
        if (dto == null) {
            return error("成品收货登记异常!登记信息不能为空");
        }
        // 调用FinishedGoodsRegisterImpl.executeProcess方法
        ProcResult procResult = finishedGoodsRegister.call(dto);
        if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
            return AjaxResult.success(procResult.getMessage());
        } else {
            return AjaxResult.error(procResult.getMessage());
        }
    }

    /**
     * 入库放行
     */
    @PostMapping("/goRelease")
    public AjaxResult wipMaterialRelease(@RequestBody WipMaterialReleaseParams dto) {
        // 校验放行信息
        if (dto == null) {
            return error("入库放行异常!请检查参数");
        }
        dto.setCreator(getUserId());
        ProcResult procResult = wipMaterialRelease.call(dto);
        if ("0".equals(procResult.getCode())) {
            return AjaxResult.success(procResult.getMessage());
        } else {
            return AjaxResult.error(procResult.getMessage());
        }
    }
}
